﻿$(function () {
    loadDepartment();
    $("#btnSave").click(function () {
        if (validate()) {
            save();

            //清空表单
            $("#DepartmentId").val(999);
            $("#Name").val("");
            $("#Tel").val("");
            $("#BZType").val("");
            $("#Address").val("");
            $("#YYTime").val("");
        }
    });

});

//此处加载所有派出所
function loadDepartment() {
    $("#DepartmentId").html();
    var option = "";

    $.ajax({
        type: "GET",
        url: "/Sys/Department/GetAllDepIsDisplay?_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data, function (i, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>"
            })
            $("#DepartmentId").append(option);
        }
    });
}


//保存
function save() {
    var postData = {
        "Id": "0",
        "DepartmentId": $("#DepartmentId").val(),
        "Name": $("#Name").val(),
        "Tel": $("#Tel").val(),
        "BZType": $("#BZType").val(),
        "Address": $("#Address").val(),
        "YYTime": $("#YYTime").val(),
        "IsBL": false,
        "Remarks": ""
    };
    $.ajax({
        type: "Post",
        url: "/YYSM/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {

                var $toast = $('#toast');
                if ($toast.css('display') != 'none') return;
                $toast.fadeIn(50);
                setTimeout(function () {
                    $toast.fadeOut(100);
                }, 2000);

            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};





function GetTime(date) {
    if (date == null) {
        date = new Date();
    }
    var y = date.getFullYear();
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    //var S = date.getTime() % 1000;
    var html = y + "-";
    if (M < 10) {
        html += "0";
    }
    html += M + "-";

    if (d < 10) {
        html += "0";
    }
    html += d + " ";
    if (h < 10) {
        html += "0";
    }
    html += h + ":";
    if (m < 10) {
        html += "0";
    }
    html += m + ":";
    if (s < 10) {
        html += "0";
    }
    html += s;
    //html += " ";
    //if (S < 100) {
    //    html += "0"
    //}
    //if (S < 10) {
    //    html += "0";
    //}
    //html += S;
    return html;
}

// 验证中文名称
function isChinaName(name) {
    var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
    return pattern.test(name);
}

//验证表单
function validate() {
    var dpartmentId = $("#DepartmentId").val();
    if (dpartmentId == 999) {
        str = '部门没有选择';
        layer.tips(str, "#btnSave");
        $("#DepartmentId").focus();
        return false;
    }

    var name = $("#Name").val();
    if ($.trim(name).length == 0) {
        str = '姓名没有输入';
        layer.tips(str, "#btnSave");
        $("#Name").focus();
        return false;
    } else {
        if (isChinaName(name) == false) {
            str = '请输入中文姓名';
            layer.tips(str, "#btnSave");
            $("#Name").focus();
            return false;
        }
    }

    var tel = $("#Tel").val();
    if ($.trim(tel).length < 6) {
        str = '联系电话没有输入';
        layer.tips(str, "#btnSave");
        $("#Tel").focus();
        return false;
    }

    var address = $("#Address").val();
    if ($.trim(address).length == 0) {
        str = '上门地址没有输入';
        layer.tips(str, "#btnSave");
        $("#Address").focus();
        return false;
    }

    var yYTime = $("#YYTime").val();
    if ($.trim(yYTime).length == 0) {
        str = '预约时间没有输入';
        layer.tips(str, "#btnSave");
        $("#YYTime").focus();
        return false;
    }

    return true;
}